package base

import (
	"log"
	"os"

	"gopkg.in/yaml.v3"
)

type H2oXcxConf struct {
	AppId string `yaml:"appid"`
	Token string `yaml:"token"`
	Api   string `yaml:"api"`
}

type H2oLogConf struct {
	Level      string `yaml:"level"`
	File       string `yaml:"log_file"`
	MaxSize    int    `yaml:"max_size"`
	MaxBackups int    `yaml:"max_backups"`
	MaxAge     int    `yaml:"max_age"`
	Env        string `yaml:"env"`
}

type H2oHttpConf struct {
	Port   int    `yaml:"port"`
	Host   string `yaml:"domain"`
	Prefix string `yaml:"path-prefix"`
	Jwt    string `yaml:"jwt"`
	Sault  string `yaml:"sault"`
	Avator string `yaml:"avator"`
	Admin  string `yaml:"admin"`
}

type H2oInfra struct {
	MyDns string `yaml:"db_dns"`
	KvDns string `yaml:"kv_dns"`
}

type H2oConf struct {
	Xcx   H2oXcxConf  `yaml:"xcx"`
	Log   H2oLogConf  `yaml:"logger"`
	Http  H2oHttpConf `yaml:"http"`
	Infra H2oInfra    `yaml:"infra"`
}

func (hconf *H2oConf) ReadConf(path string) {
	ymlFile, err := os.ReadFile(path)
	if err != nil {
		log.Printf("ReadFile failed:%s", err.Error())
		panic(err)
	}
	err = yaml.Unmarshal(ymlFile, hconf)
	if err != nil {
		log.Printf("Unmarshal failed:%s", err.Error())
		panic(err)
	}
}
